// @ts-nocheck
import router from '@system.router';
import storage from '@system.storage';
import mock from '../../common/json/mock.json'
import prompt from '@system.prompt';

export default {
    data: {
        title: "",
        checkboxVisible: false,
        selectedNote: [],
//        toDoList: [],
        toDoList: [
            {
                "title": "今日计划",
                "content": "这个是今日计划标题",
                "date": "2021-06-08"
            },
            {
                "title": "昨天计划",
                "content": "这个是昨天的计划",
                "date": "2021-06-07"
            },
            {
                "title": "前天计划",
                "content": "这个是前天的计划",
                "date": "2021-06-05"
            }
        ],
    },
    onInit() {

    },
    onCreate() {

    },
    onShow() {
        this.initData();
    },

/************** 自定义的方法区 ****************/
    toDetails(index, obj) {
        obj.index = index;
        router.push({
            uri: 'pages/details/details',
            params: {
                noteInfo: obj,
                mode:'edit'
            }
        });
    },
    createNote(){
        router.push({
            uri: 'pages/details/details',
            params: {
                mode:'add'
            }
        });
    },
    showCheckBox() {
        this.checkboxVisible = !this.checkboxVisible;
        if (!this.checkboxVisible) {
            this.selectedNote = []
        }
        if (index != null) {
            console.log("打印选中标题："+this.toDoList[index].content);
            this.toDoList[index].selected = true;
        }
    },
    searchNote(val){
        console.log("搜索值："+value);
    },
    selectNote(index) {
        console.log("选择笔记索引：" + index);
        //检查数组中是否已经存在此索引，存在则执行去除操作，不存在则执行添加操作
        if (this.selectedNote.indexOf(index) != -1) {
            this.selectedNote.splice(index, 1)
        }
        else {
            this.selectedNote.push(index);
        }
    },
    deleteNoteBatch() {
        let that = this;
        prompt.showDialog({
            title: '操作确认',
            message: '确定要删除选中的便签吗？',
            buttons: [
                {
                    text: '删除',
                    color: 'red',
                },
                {
                    text: '取消',
                    color: '#ffa704',
                },
            ],
            success: function (data) {
                //点击确定按钮后执行操作
                if (data.index === 0) {
                    for (let i = that.toDoList.length - 1; i >= 0; i--) {
                        if (that.selectedNote[i] != null) {
                            that.toDoList.splice(that.selectedNote[i], 1);
                        }
                    }
                    //重置多选框和数组
                    that.checkboxVisible = false;
                    that.selectedNote = [];
                    //将数据持久化存储
                    storage.set({
                        key: 'toDoList',
                        value: JSON.stringify(that.toDoList)
                    });
                }
            },
        });

    },
    initData() {
        let that = this;
        //如果是初次初始化，则从json文件里读取数据
        storage.get({
            key: 'init',
            success: function (data) {
                if (!data && data != 'false') {
                    console.log('没有数据，从json文件中进行初始化');
                    storage.set({
                        key: 'toDoList',
                        value: JSON.stringify(mock.toDoList)
                    });
                    storage.set({
                        key: 'init',
                        value: "false"
                    });
                    that.toDoList = mock.toDoList;
                }
                else {
                    console.log("从storge中加载数据");
                    storage.get({
                        key: 'toDoList',
                        success: function (data) {
                            that.toDoList = JSON.parse(data);
                        }
                    });
                }
            }
        })
    }
}
